package ru.alexbykov.pagination.adapters;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import ru.alexbykov.pagination.ResourceTable;

/**
 * 列表适配器
 *
 * @since 2021-06-29
 */
public class TestAdapter extends BaseItemProvider {
    private static final int LAYOUT = ResourceTable.Layout_item_layout;
    private List<Integer> items;

    public TestAdapter() {
        items = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public Object getItem(int position) {
        if (items != null && position >= 0 && position < items.size()) {
            return items.get(position);
        }
        return Optional.empty();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component view = LayoutScatter.getInstance(componentContainer.getContext())
            .parse(LAYOUT, null, false);
        Text tvNumber = (Text) view.findComponentById(ResourceTable.Id_tvNumber);
        tvNumber.setText(items.get(position) + "");
        return view;
    }

    /**
     * 添加数据
     *
     * @param items 数据
     */
    public void addItems(List<Integer> items) {
        this.items.addAll(items);
        notifyDataChanged();
    }
}
